/**
 * Created by Administrator on 2017/1/1.
 */
//有道翻译
function translate_sent() {
    var source_sentence=$("#source").val();
    var typeNum=$("#type").prop("selected",true).val();
    $.ajax({
        url:"/index/?type=train&page=1",
        type:"POST",
        data:{"source":source_sentence,"typenum":typeNum},
        success:function (data) {
            $("#train_result").text(data);
        }
    })
}
//查找相关的内容
function searchword() {
    var search_req={};
    search_req["search"]=$("#search").val();

    $.ajax({
        url:"/index/?type=search&page=1",
        type:"POST",
        data: search_req,
        dataType:"json",
        success:function (data) {

            var table_html=null;
            for(var row in data){

                table_html+="<tr>" +
                        "<td style='display: none'>"+data[row]["pk"]+"</td>" +
                        "<td>"+data[row]["fields"]["word"]+"</td>" +
                        "<td>"+data[row]["fields"]["explanation"]+"</td>" +
                        "<td><a href='javascript:void(0)' onclick='showmark(this)'>查看备注</a></td>" +
                        "<td>"+"<a href='javascript:void(0)' onclick='deleterow(this)'>删除</a>"+"</td>" +
                        "</tr>";
            }
            $("#search_result").empty();
            $("#search_result").append(table_html);
        }
    })
}
//删除一条记录
function deleterow(item) {
    var id=$(item).parent().siblings().eq(0).text();
    $.ajax({
        url:"/index/?type=delete&page=1",
        type:"POST",
        data:{"id":id},
        dataType:"json",
        success:function (data) {
            if(data["flag"]==true){
                location.href="/index/";
            }
        }
    });
}
//删除所有记录
function deleteCheck() {
    var html="<div class='filter'></div><div class='dialog'>" +
                    "<p>是否要删除全部</p>"+
                    "<div><input type='button' value='是' id='yes' onclick='deleteall();' />" +
                    "<input type='button' value='否' id='no' onclick='closeDialog();' /></div>" +
            "</div>";
    $("body").append(html);
    console.log($(".dialog").height())
}
//关闭对话框
function closeDialog() {
    $(".dialog").remove();
    $(".filter").remove();
}
//删除所有
function deleteall() {
    $.ajax({
        url:"/index/?type=deleteall",
        type:"GET",
        dataType:"json",
        success:function (data) {
            if(data["flag"]==true){
                location.href="/index/";
            }
        }
    })
}
//显示修改备注
function showmark(item){

    var id=$(item).parent().siblings().eq(0).text();
    var content=null;
    $.ajax({
        url:"/index/?type=showmark",
        type:"POST",
        data:{"id":id},
        dataType:"json",
        success:function (data) {
            content=data["remark"];
            $(item).parent().append("<textarea name='changemark'>"+content+"</textarea><a href='javascript:void(0)' onclick='setchange(this)'>修改</a>");

            $(item).parent().parent().siblings().find("textarea").each(function () {
                var id=$(this).parent().siblings().eq(0).text();
                content=$(this).val();
                $.ajax({
                    url:"/index/?type=remark",
                    type:"POST",
                    data:{"id":id,"remark":content},
                    success:function (data) {

                    }
                })
            });
            var temp=$(item).parent().parent().siblings().find("textarea").parent();
            console.log(temp);
            temp.empty();
            temp.append("<a href='javascript:void(0)' onclick='showmark(this)'>查看备注</a>");
            $(item).remove();
        }
    });

}
function setchange(item){
    var id=$(item).parent().siblings().eq(0).text();
    content=$(item).siblings().val();
    $.ajax({
        url: "/index/?type=remark",
        type: "POST",
        data: {"id":id,"remark":content},
        dataType: "json",
        success:function (data) {

        }

    });
    var temp=$(item).parent();
    console.log(temp);
    temp.empty();
    temp.append("<a href='javascript:void(0)' onclick='showmark(this)'>查看备注</a>");

}